package controller;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import util.AjaxResultDto;
import util.CommonConstant;
import util.CommonSendRequestUtil;
import util.KKClient;
import util.KKResponse;

import com.alibaba.fastjson.JSONObject;

@Controller
public class ManageController {
	/**
	 * PageManage(获取管理页面)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param request
	 * @param response
	 * @return
	 *ModelAndView
	 * @exception
	 * @since  1.0.0
	*/
	@RequestMapping ("/PageManage")  
    public ModelAndView PageManage(HttpServletRequest request, HttpServletResponse response) throws Exception {  
       ModelAndView modelAndView = new ModelAndView();  
       modelAndView.setViewName( "PageManage.jsp" );  
       return modelAndView;  
    }  
	
	/**
	 * getProjects(获取用户项目)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 *ModelAndView
	 * @exception
	 * @since  1.0.0
	*/
	@RequestMapping ("/getProjects")  
	public ModelAndView getProjects(HttpServletRequest request, HttpServletResponse response) throws Exception {  
		//1.获取用户工程
		String getUrl = CommonConstant.baseUrl +"/user/projects";
		//2.发送请求
		HttpSession session = request.getSession(); 
		String token = (String) session.getAttribute("token");
		KKResponse resp = CommonSendRequestUtil.sendRestGetRequest(getUrl, null, token);
		String responseBody = resp.getResponseBody();
		//JSONObject result = KKClient.sendGETRequest(getUrl,token,null);
		PrintWriter out = response.getWriter();
		//3.返回结果
		out.print(responseBody);
		out.flush();
		out.close();
        return null;  
	}  
	
	/**
	 * getProjectById(根据项目id获取用户项目)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 *ModelAndView
	 * @exception
	 * @since  1.0.0
	*/
	@RequestMapping ("/getProjectById")  
	public ModelAndView getProjectById(HttpServletRequest request, HttpServletResponse response) throws Exception {  
		//1.获取用户工程
		String projectId = request.getParameter("projectId");
		String getUrl = CommonConstant.baseUrl +"/project/id/" + projectId;
		//2.发送请求
		HttpSession session = request.getSession(); 
		String token = (String) session.getAttribute("token");
		KKResponse resp = CommonSendRequestUtil.sendRestGetRequest(getUrl, null, token);
		String responseBody = resp.getResponseBody();
		responseBody = responseBody.replaceAll("\\\\\"", "'");
		//JSONObject result = KKClient.sendGETRequest(getUrl,token,null);
		PrintWriter out = response.getWriter();
		//3.返回结果
		out.print(responseBody);
		out.flush();
		out.close();
        return null;  
	}  
	
	/**
	 * addProject(新建工程)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param request
	 * @param response
	 * @return
	 *ModelAndView
	 * @exception
	 * @since  1.0.0
	*/
	@RequestMapping ("/addProject")  
	public ModelAndView addProject(HttpServletRequest request, HttpServletResponse response) throws Exception {  
		//1.获取工程
		String project = request.getParameter("project");
		String postUrl = CommonConstant.baseUrl +"/project";
		JSONObject obj = JSONObject.parseObject(project);

		//2.发送请求
		HttpSession session = request.getSession(); 
		String token = (String) session.getAttribute("token");
		KKResponse resp = CommonSendRequestUtil.sendRestPostRequest(postUrl, obj, token);
		String responseBody = resp.getResponseBody();
		//JSONObject result = KKClient.sendGETRequest(getUrl,token,null);
		PrintWriter out = response.getWriter();
		//3.返回结果
		out.print(responseBody);
		out.flush();
		out.close();
        return null;  
	}  
	
	/**
	 * updateProject(更新工程)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param request
	 * @param response
	 * @return
	 *ModelAndView
	 * @exception
	 * @since  1.0.0
	*/
	@RequestMapping ("/updateProject")  
	public ModelAndView updateProject(HttpServletRequest request, HttpServletResponse response) throws Exception {  
		//1.获取工程
		String project = request.getParameter("project");
		String test= request.getParameter("test");
		JSONObject obj = JSONObject.parseObject(project);
		String projectId = obj.getString("id");
		String postUrl = CommonConstant.baseUrl +"/project/id/" + projectId;
		//2.发送请求
		HttpSession session = request.getSession();
		String token = (String) session.getAttribute("token");
		KKResponse resp = CommonSendRequestUtil.sendRestPutRequest(postUrl, obj, token);
		String responseBody = resp.getResponseBody();
		//JSONObject result = KKClient.sendGETRequest(getUrl,token,null);
		PrintWriter out = response.getWriter();
		//3.返回结果
		out.print(responseBody);
		out.flush();
		out.close();
        return null;  
	}
	
	/**
	 * deleteProject(删除工程)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param request
	 * @param response
	 * @return
	 *ModelAndView
	 * @exception
	 * @since  1.0.0
	*/
	@RequestMapping ("/deleteProject")  
	public ModelAndView deleteProject(HttpServletRequest request, HttpServletResponse response) throws Exception {  
		//1.获取工程
		String projectId = request.getParameter("projectId");
		String postUrl = CommonConstant.baseUrl +"/project/id/" + projectId;
		//2.发送请求
		HttpSession session = request.getSession();
		String token = (String) session.getAttribute("token");
		KKResponse resp = CommonSendRequestUtil.sendRestDeleteRequest(postUrl, null, token);
		String responseBody = resp.getResponseBody();
		//JSONObject result = KKClient.sendGETRequest(getUrl,token,null);
		PrintWriter out = response.getWriter();
		//3.返回结果
		out.print(responseBody);
		out.flush();
		out.close();
        return null;  
	}
	
}
